﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_website_system_icon_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.hidTable.Value = Request.QueryString["tbname"];
                this.hidId.Value = Request.QueryString["id"];
                this.txtName.Text = Request.QueryString["name"];
                this.txtIconUrl.Text = Request.QueryString["iconurl"];
                this.imgview.Src = "../" + this.txtIconUrl.Text;
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            if (fileUpIcon.HasFile && this.hidId.Value != string.Empty && this.hidTable.Value != string.Empty)
            {
                string sIconUrl = string.Empty;
                string fullname = this.fileUpIcon.PostedFile.FileName;
                string ext = fullname.Substring(fullname.LastIndexOf(".") + 1).ToLower();
                if (ext == "jpg" || ext == "gif" || ext == "bmp" || ext == "png")
                {
                    string sSaveFolder = Server.MapPath("~/uploadpic");
                    string sNewFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + PM.Web.Common.GetRandomNumber(5) + "." + ext;
                    try
                    {
                        if (!System.IO.Directory.Exists(sSaveFolder)) System.IO.Directory.CreateDirectory(sSaveFolder);
                        this.fileUpIcon.SaveAs(sSaveFolder + "/" + sNewFileName);
                        sIconUrl = "uploadpic/" + sNewFileName;
                        DataAccess.PmData dal = new DataAccess.PmData();
                        if (dal.ModifySystemIcon(this.hidTable.Value, int.Parse(this.hidId.Value), sIconUrl))
                        {
                            PM.Web.Common.JsAlert(this, "修改成功！",true);
                        }
                        else
                        {
                            PM.Web.Common.JsAlert(this, "修改失败！",true);
                        }
                    }
                    catch
                    {
                        PM.Web.Common.Alert(this, "上传图片失败！");
                    }
                }
                else
                {
                    PM.Web.Common.Alert(this, "只能上传图片！");
                }
            }
        }
    }
}